package com.xboss.tv.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.xboss.tv.R;

/**
 * Created by Administrator on 2017\7\21 0021.
 */

public class MenuAdapter extends RecyclerView.Adapter<MenuAdapter.MenuHolder> {

    private Context mContext;
    private LayoutInflater mInflater;
    private String[] titles = new String[]{"要闻", "财经", "音乐", "体育", "科技", "影视", "汽车"
            , "游戏", "教育", "儿童", "旅游", "时尚", "美食", "家电", "家居", "房产"};

    public OnMenuitemClickListener mClickListener;

    public interface OnMenuitemClickListener {
        public void onItemClick(String name, int position);
    }

    public void setOnMenuitemClickListener(OnMenuitemClickListener onMenuitemClickListener) {
        mClickListener = onMenuitemClickListener;
    }

    public MenuAdapter(Context context) {
        mContext = context;
        mInflater = LayoutInflater.from(context);
    }

    @Override
    public MenuHolder onCreateViewHolder(ViewGroup parent, int viewType) {

        View view = mInflater.inflate(R.layout.item_menu, parent, false);

        return new MenuHolder(view);
    }

    @Override
    public void onBindViewHolder(MenuHolder holder, final int position) {

        holder.mTextName.setText(titles[position]);
        holder.mLinear.setFocusable(true);
        holder.mLinear.setOnKeyListener(new View.OnKeyListener() {
            @Override
            public boolean onKey(View v, int keyCode, KeyEvent event) {

                if (keyCode == KeyEvent.KEYCODE_ENTER) {

                    mClickListener.onItemClick(titles[position], position);
                    return false;
                }
                return false;
            }
        });
        holder.mLinear.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mClickListener.onItemClick(titles[position], position);
            }
        });
    }

    @Override
    public int getItemCount() {
        return titles.length;
    }

    class MenuHolder extends RecyclerView.ViewHolder {

        public TextView mTextName;
        public LinearLayout mLinear;

        public MenuHolder(View itemView) {
            super(itemView);
            mLinear = (LinearLayout) itemView.findViewById(R.id.item_menu_linear);
            mTextName = (TextView) itemView.findViewById(R.id.item_menu_text_name);
        }
    }
}
